/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.bankroll.poker.view.grafico;

import br.com.bankroll.poker.bean.BankRoll;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataItem;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

/**
 * Grafico de temperatura
 *
 * @author Paula
 */
public final class Grafico extends ApplicationFrame {

    private static final long serialVersionUID = 1L;
    private static final Dimension DIMENSION = new Dimension(550, 370);    
    private static final int INICIALIZADOR = 1;
    private static final int ULTIMOS_DEZ = 10;
    private static XYSeries xYSeries = new XYSeries("Temperatura Medida");
    private static XYSeriesCollection seriesCollection = null;
    private static List<Double> listaTemperatura = new ArrayList<Double>();

    /**
     * Grafico JFreeChart
     *
     * @param title titulo do frame
     */
    public Grafico(final String title) {
        super(title);
        super.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

    }

    /**
     * Criação do Chart
     *
     * @param XYDataset dataset.
     * @return um chart.
     */
     public static JFreeChart createChart(final XYDataset dataset) {
        
              
        final JFreeChart chart = ChartFactory.createXYLineChart(
            "Medidor de temperatura de aquários",      // Titulo
            "Medicoes",                                 // eixo x
            "Temperatura",                             // eixo y
            dataset,                                   // data
            PlotOrientation.VERTICAL,
            true,                                      // incluir legenda
            true,                                      // tooltips
            false                                      // urls
        );

       
        chart.setBackgroundPaint(Color.white);
        
        
        final XYPlot plot = chart.getXYPlot();
        plot.setBackgroundPaint(Color.lightGray);
        plot.setDomainGridlinePaint(Color.white);
        plot.setRangeGridlinePaint(Color.white);
        
        final XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
        renderer.setSeriesLinesVisible(1, false);
        renderer.setSeriesShapesVisible(1, false);
        plot.setRenderer(renderer);
        
        plot.setDomainCrosshairLockedOnData(true);
        plot.setDomainCrosshairVisible(true);
        plot.setRangeCrosshairLockedOnData(true);
        plot.setRangeCrosshairVisible(true);
        

     
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

                
        return chart;
        
    }

    public static void abrirGrafico() {
        BankRoll bankroll = new BankRoll();
        RefineryUtilities.centerFrameOnScreen();
        try {
                final XYDataset dataset = recuperarDados(bankroll);
                final JFreeChart chart = createChart(dataset);
                final ChartPanel chartPanel = new ChartPanel(chart);
                chartPanel.setPreferredSize(DIMENSION);
                chartPanel.setMouseZoomable(true, true);

            super.setContentPane(chartPanel);
                super.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
                super.validate();

            } catch (Exception exception) {
                System.out.print(exception.getMessage());
            }
        grafic.pack();
        grafic.setVisible(true);
        grafic.setSize(DIMENSION);

    }

    public static XYSeriesCollection recuperarDados(BankRoll bankroll) {

        try {

            seriesCollection = new XYSeriesCollection();

            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
            listaTemperatura.add((int) 3 + (Math.random() * 45));
                        
      /*      if (listaTemperatura.size() > ULTIMOS_DEZ) {
                listaTemperatura.remove(0);
            }*/
            xYSeries = new XYSeries("Temperatura Medida");
            for (int contador = 0; contador < ULTIMOS_DEZ; contador++) {
               double temperatura;
              
               if(contador == ULTIMOS_DEZ){
                   temperatura = listaTemperatura.get(contador-INICIALIZADOR);
                }else{ 
                   try{
                       temperatura = listaTemperatura.get(contador);
                   }catch(Exception e){
                       temperatura = 0;
                   }
                }

                int ultimoDez = contador + INICIALIZADOR;
                xYSeries.add(new XYDataItem(ultimoDez, temperatura));

            }

     
        } catch (Exception e) {
            System.err.println(e.getMessage() + e);
        }
        seriesCollection.addSeries(xYSeries);


        return seriesCollection;
    }
    
    /**
     * Sobreposição do método windowClosing para poder fechar apenas o
     * JFreeChart
     *
     * @param WindowEvent e
     */
    @Override
    public void windowClosing(WindowEvent e) {
        this.setVisible(false);

    }
}